package CtrLayer;
import java.util.Iterator;
import ModelLayer.Customer;
import ModelLayer.CustomerList;

public class CustomerCtr
{
    private static CustomerList instance = CustomerList.getInstance();

    public CustomerCtr()
    {
    	createCustomer(997, "John", "Sofiendalsvej 60", "9200", "415265", "a@a.c", "258","Silver");
    	createCustomer(998, "Dima", "Sofiendalsvej 45", "9200", "457125", "a@b.c", "451", "Premium");
    	createCustomer(999, "Vansil", "Sofiendalsvej 23", "9200", "508941", "a@d.c","945", "Classic");
    	createCustomer(0,"Default customer","empty","empty","empty","empty","empty","empty");
   }

    public void createCustomer(int id, String name, String address, String postCode, String phone, String email, String cvr, String type)
    {
        Customer newCustomer = new Customer(id, name, address, postCode, phone, email, cvr, type);
        instance.addCustomer(newCustomer);
    }

    public Customer getCustomer(int id) 
    {
        return instance.getCustomer(id);
    }
    
    public Customer getCustomer(String name) 
    {
    	try{
    		int idC = Integer.parseInt(name);
    		return this.getCustomer(idC);
    	}
    	catch(Exception ex){}
    	
    	Iterator it = this.getCustomersIterator();
    	while(it.hasNext())
    	{
    		Customer cust = (Customer)it.next();
    		if(cust.getName().equals(name)) return cust;
    	}
        return null;
    }
    
    public Iterator getCustomersIterator()
    {
        return instance.getCustomersIterator();
    }

    public int getCustomerSize()
    {
    	return instance.getCustomerSize();
    }
    
    public boolean updateCustomer(int id, String updateValue, int opt)
    {
        Customer cust = getCustomer(id);
        try{
            switch(opt)
            {
                case 1 : { cust.setName(updateValue); } break;
                case 2 : { cust.setAddress(updateValue); } break;
                case 3 : { cust.setPostCode(updateValue); } break;
                case 4 : { cust.setPhone(updateValue); } break;
                case 5 : { cust.setEmail(updateValue); } break;
                case 6 : { cust.setCvr(updateValue); } break;
                case 7 : { cust.setType(updateValue); } break;
            }
            return true;
        }
        catch(Exception e) {}
        return false;
    }

    public boolean deleteCustomer(int id)
    {
        return instance.deleteCustomer(id);
    }
}
